package com.eii.common.web.domain;

import com.google.common.collect.ImmutableMap;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import java.util.Map;

/**
 * 公共状态枚举
 **/
public enum ResultCode implements IResultCode {
    /**
     * 操作成功,与预期相符
     */
    SUCCESS(HttpStatus.OK, "操作成功"),
    /**
     * 操作失败,与预期不符
     */
    FAILURE(HttpStatus.INTERNAL_SERVER_ERROR, "操作失败"),
    /**
     * 请求参数错误(缺少参数，参数类型错误，参数校验未通过)
     */
    ERROR_PARAMETER(HttpStatus.BAD_REQUEST, "请求参数错误"),
    /**
     * 404 没找到请求
     */
    NOT_FOUND(HttpStatus.NOT_FOUND, "404 没找到请求"),
    /**
     * 未登录
     */
    NOT_LOGIN(HttpStatus.UNAUTHORIZED, "此操作需要登陆系统"),
    /**
     * 已过期
     */
    EXPIRED(HttpStatus.METHOD_NOT_ALLOWED, "已过期"),
    /**
     * 未通过验证
     */
    NO_AUTH(HttpStatus.NOT_ACCEPTABLE, "未通过验证"),
    /**
     * 缺少操作权限
     */
    NO_ACCESS(HttpStatus.FORBIDDEN, "无权限"),
    /**
     * 重复请求
     */
    REPEAT_REQUEST(HttpStatus.LOCKED, "重复请求"),
    /**
     * 服务器繁忙
     */
    BUSY_SERVER(HttpStatus.SERVICE_UNAVAILABLE, "抱歉，系统繁忙，请稍后重试"),
    /**
     * 服务器异常
     */
    ERROR_SERVER(HttpStatus.NOT_IMPLEMENTED, "服务器异常");

    ResultCode(HttpStatus httpStatus, String desc) {
        this.code = httpStatus.value();
        this.httpStatus = httpStatus;
        this.desc = desc;
    }

    /**
     * 自定义状态码
     */
    @Getter
    int code;
    /**
     * http状态码
     */
    @Getter
    private HttpStatus httpStatus;
    /**
     * 描述
     */
    @Getter
    String desc;

    private static final Map<Integer, ResultCode> enumMap;

    static {
        ImmutableMap.Builder<Integer, ResultCode> builder = ImmutableMap.<Integer, ResultCode>builder();
        for (ResultCode iter : ResultCode.values()) {
            builder.put(iter.code, iter);
        }
        enumMap = builder.build();
    }

    public static ResultCode of(Integer value) {
        return enumMap.get(value);
    }
}
